package com.example.demo.controller;


import com.example.demo.commen.AppResult;
import com.example.demo.exception.ApplicationException;
import com.example.demo.model.Message;
import com.example.demo.model.User;
import com.example.demo.service.impl.MessageServiceImpl;
import com.example.demo.service.impl.UserServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Api(tags = "私信接口")
@RestController
@RequestMapping("/message")
@Slf4j
public class MessageController {

    @Autowired
    MessageServiceImpl messageService;

    @Autowired
    UserServiceImpl userService;

    @ApiOperation("发私信")
    @RequestMapping(method = RequestMethod.POST,value = "/sendMessage")
    public AppResult sendMessage(HttpServletRequest request,
                                 @ApiParam("接收用户")@RequestParam("receiveUserId")@NonNull Long receiveUserId,
                                 @ApiParam("私信内容")@RequestParam("content")@NonNull String content){

                     if(receiveUserId==null||receiveUserId<=0||content==null||!StringUtils.hasLength(content)){
                         throw new ApplicationException("参数校验异常");
                     }
             User receiveUser = userService.getUserById(receiveUserId);
                     if(receiveUser==null||receiveUser.getDeleteState()==1||receiveUser.getId()<=0){
                         throw new ApplicationException("用户不存在");
                     }
                     HttpSession session = request.getSession(false);
                     User postUser = (User) session.getAttribute("userInfo");
                     if(postUser==null||postUser.getId()<=0||postUser.getDeleteState()==1||postUser.getState()==1){
                         throw new ApplicationException("用户不存在或被禁言了");
                     }
                     if(postUser.getId()==receiveUser.getId()){
                         throw  new ApplicationException("不可以给自己发私信");
                     }

                     Message message = new Message();
                     message.setPostUserId(postUser.getId());
                     message.setReceiveUserId(receiveUser.getId());
                     message.setContent(content);
                     messageService.insertMessage(message);
                     return AppResult.success("发送成功");
    }

    @ApiOperation("获取当前登录用户的站内信未读数")
    @RequestMapping(method = RequestMethod.GET,value = "/getUnReadCount")
    public AppResult<Integer>  getUnReadMessageCount(HttpServletRequest request){

        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute("userInfo");
        if(user==null||user.getId()<=0||user.getDeleteState()==1){
            throw new ApplicationException("用户不存在");
        }

        Integer unReadMessageCount = messageService.getUnReadMessage(user.getId());
        log.info(String.valueOf(unReadMessageCount));
        return AppResult.success(unReadMessageCount);
    }

    @ApiOperation("查看当前用户的站内信")
    @RequestMapping(method = RequestMethod.GET,value = "/getMessageAll")

    public AppResult<List<Message>> getMessageByReceiveId(HttpServletRequest request
                                                          ){

               HttpSession session  = request.getSession(false);
               User user = (User) session.getAttribute("userInfo");
               if(user==null||user.getId()<=0||user.getDeleteState()==1){
                   throw new ApplicationException("用户不存在");
               }

               List<Message> lists = messageService.getMessageByReceiveId(user.getId());
               return AppResult.success(lists);

    }

    @ApiOperation("更新站内信状态为已读")
    @RequestMapping(method = RequestMethod.POST,value = "/updateState")
    public AppResult updateMessageState(HttpServletRequest request,
                                        @ApiParam("站内信Id")
                                       @RequestParam(value = "messageId",required = true) Long messageId
                                      ){

        if(messageId==null||messageId<=0){
            throw  new ApplicationException("帖子不存在或者状态出错");
        }

        Message message = messageService.selectMessageById(messageId);
        if(message==null||message.getId()<=0||message.getDeleteState()==1){
            throw new ApplicationException("帖子不存在");
        }

        //判断站内信是不是当前用户的

        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute("userInfo");
        if(user==null||user.getId()<=0||user.getDeleteState()==1){
            throw new ApplicationException("用户不存在");
        }
        if(user.getId()!=message.getReceiveUserId()){
            throw new ApplicationException("此站内信不是您的");
        }
        messageService.updateMessageState(messageId,(byte)1);   //更新为已读
        return AppResult.success("更新指定站内信状态成功");
    }
}
